package com.wn.common.util;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.Properties;

public class MailUtil {
    public static void sendEmail(String email,String code) {
        try {
            final Properties props = new Properties();
            props.put("mail.user","2871405505@qq.com");
            props.put("mail.password","wktzhphiqfdhdejf"); //授权码
            props.put("mail.smtp.auth","true");
            props.put("mail.transport.protocal","smtp");
            props.put("mail.smtp.host","smtp.qq.com");
            props.put("mail.smtp.port","25");

            Session mailSession = Session.getDefaultInstance(props);
            Message msg = new MimeMessage(mailSession);
            msg.setFrom(new InternetAddress("2871405505@qq.com"));
            msg.addRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
            msg.setSubject("用户验证邮件-飓风旅游");
            msg.setContent("<h2>此邮件为飓风旅游平台验证邮件！</h2><br>"+
                    "此邮件30分钟有效！请勿将此验证码告诉他人！验证码："+"<h3>"+code+"</h3>","text/html;charset=UTF-8");

            msg.saveChanges();

            Transport transport = mailSession.getTransport("smtp");
            transport.connect(props.getProperty("mail.smtp.host"),props.getProperty("mail.user"),props.getProperty("mail.password"));
            transport.sendMessage(msg,msg.getAllRecipients());
            transport.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }
}
